//
//  TYMBVoiceCircleView.m
//  TY_MotorRemoteControl
//
//  Created by 徐伟新 on 2024/3/22.
//  Copyright © 2024 zhongdaodianzi. All rights reserved.
//

#import "TYMBVoiceCircleView.h"

@interface TYMBVoiceCircleView ()

@property (nonatomic, strong) CAShapeLayer *blackLayer;
@property (nonatomic, strong) CAShapeLayer *greenLayer;

@end

@implementation TYMBVoiceCircleView

- (void)drawRect:(CGRect)rect {
    
    
    if (!_blackLayer) {
        [self.layer addSublayer:self.blackLayer];
    }
    CGPoint point = [self getAngleWithStartPoint:CGPointMake(self.width/2.0, self.height) withCenterPoint:CGPointMake(self.width/2.0, self.height/2.0-5.0) withnRadius:M_PI_4];
    CGPoint point1 = [self getAngleWithStartPoint:CGPointMake(self.width/2.0, self.height) withCenterPoint:CGPointMake(self.width/2.0, self.height/2.0-5.0) withnRadius:(2*M_PI -M_PI_4)];
    
    self.subBtn.center = point;
    self.addBtn.center = point1;

}

- (CAShapeLayer *)blackLayer {
    if (!_blackLayer) {
        _blackLayer = [CAShapeLayer layer];
        _blackLayer.path = [UIBezierPath
                                   bezierPathWithArcCenter:CGPointMake(self.frame.size.width * 0.5, self.frame.size.height * 0.5)
                                   radius:self.width/2.0-5.0
                                   startAngle:self.startAngle
                                   endAngle:self.endAngle
                                   clockwise:1].CGPath;
        _blackLayer.fillColor = [UIColor clearColor].CGColor;
        _blackLayer.strokeColor = [UIColor lightGrayColor].CGColor;
        _blackLayer.lineWidth = 10.f;
        _blackLayer.lineCap = kCALineCapSquare;
        _blackLayer.opacity = 0.8;
        
        

    }
    return _blackLayer;
}


- (CGFloat)startAngle {
    return M_PI_2 +M_PI_4;
}

- (CGFloat)endAngle {
    return M_PI_4;
    
}

- (UIButton *)subBtn {
    if (!_subBtn) {
        _subBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 40, 40)];
        [_subBtn setImage:[UIImage imageNamed:@"Reduced volume"] forState:UIControlStateNormal];
        [self addSubview:_subBtn];
    }
    return _subBtn;
}

- (UIButton *)addBtn {
    if (!_addBtn) {
        _addBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 40, 40)];
        [_addBtn setImage:[UIImage imageNamed:@"Increased volume"] forState:UIControlStateNormal];
        [self addSubview:_addBtn];
    }
    return _addBtn;
}

- (CGPoint)getAngleWithStartPoint:(CGPoint)startPoint withCenterPoint:(CGPoint)centerPoint withnRadius:(CGFloat)angle {
    
    CGFloat sinX = sinf(M_PI_4/M_PI*180.0);
    CGFloat sinY = cosf(M_PI_4/M_PI*180.0);
    CGFloat height = startPoint.y -centerPoint.y;
    CGFloat dx = 0;
    CGFloat dy = centerPoint.y+height*sinY;
    if (angle > M_PI_2) {
        dx = centerPoint.x+height*sinX;
    }else {
        dx = centerPoint.x-height*sinX;
    }
    NSLog(@"%f === %f",dx,dy);
    return CGPointMake(dx, dy);
    
       
    
    
    
}

@end
